<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property integer $id
 * @property string $email
 * @property string $username
 * @property string $fullname
 * @property string $password
 * @property boolean $isadmin
 * @property integer $logins
 * @property string $last_login
 */
class Users extends CActiveRecord
{
    public $password1;
    public $password2;
    
    private $salt = '1928';
    
    /**
	 * Returns the static model of the specified AR class.
	 * @return users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('email, fullname, username', 'required'),
			array('password1, password2', 'required', 'on' => 'chpasswd, create'),
			array('email', 'length', 'max'=>127),
			array('username', 'length', 'max'=>32),
			array('fullname', 'length', 'max'=>255),
			array('password', 'length', 'max'=>50),
			array('isadmin, last_login', 'safe'),
			array('email', 'email'),
			array('password1', 'compare', 'compareAttribute' => 'password2', 'on' => 'chpasswd, create'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('email, username, fullname', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Идентификатор',
			'email' => 'Адрес e-mail',
			'username' => 'Login',
			'fullname' => 'Полное имя',
			'password1' => 'Пароль',
			'password2' => 'Пароль еще раз',
			'isadmin' => 'Администратор',
			'last_login' => 'Последний вход в систему',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('email',$this->email,true);

		$criteria->compare('username',$this->username,true);

		$criteria->compare('fullname',$this->fullname,true);

		return new CActiveDataProvider('users', array(
			'criteria' => $criteria,
		));
	}
	
    /**
     * Crypts password using defined salt
     * @return String hashed password
     */
    private function hashPassword($password)
    {
        return md5($this->salt.$password);
    }
    
	public function validatePassword($password)
    {
        return $this->hashPassword($password) === $this->password;
    }
    
    /**
     * Makes password crypted before saved to DB
     * @return Boolean result of operation
     */
    protected function beforeSave()
    {
        if ($this->isNewRecord || $this->scenario == 'chpasswd')
        {
            $this->password = $this->hashPassword($this->password1);
        }
        
        return parent::beforeSave();
    }
}